package com.taxigang.client.uibinder;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ArticleRatingWidget extends VerticalPanel {

	String articleId;
	String starNoVotesURL = "images/index.png";  //star0.gif
	String starHoverURL = "images/index.png"; //
	String starSelectedURL = "images/index.png"; //star1.gif
	String starEmptyURL = "images/black.png";
	String starHalfURL = "images/starhalf.gif";
	Label statsLabel = null;
	boolean showStatsLabel = false;
	boolean awarded = false;
	Integer vote;
	boolean showTooltip = true;
	HorizontalPanel starPanel = new HorizontalPanel();
	Image star1 = null;
	Image star2 = null;
	Image star3 = null;
	Image star4 = null;
	Image star5 = null;
	AggregateArticleRating atr = new AggregateArticleRating();

	public int getVote(){
		return vote;
	}
	ClickListener starClickListener = new ClickListener() {

		public void onClick(Widget sender) {
			if (!atr.userVoted) {
				// do vote

				if (sender == star1) {
					vote = 1;
					atr.vote = 1;
				} else if (sender == star2) {
					vote = 2;
					atr.vote = 2;
				} else if (sender == star3) {
					vote = 3;
					atr.vote = 3;
				} else if (sender == star4) {
					vote = 4;
					atr.vote = 4;
				} else if (sender == star5) {
					vote = 5;
					atr.vote = 5;
				}
				atr.userVoted = true;
				//setArticleRating(vote);
			}
		}
	};
	MouseListener starListener = new MouseListener() {

		public void onMouseDown(Widget sender, int x, int y) {
		}

		public void onMouseEnter(Widget sender) {
			if (!atr.userVoted) {
				starMoused(sender, starHoverURL);
			}
		}

		public void onMouseLeave(Widget sender) {
			if (!atr.userVoted) {
				setStars(atr);
			}
		}

		public void onMouseMove(Widget sender, int x, int y) {
		}

		public void onMouseUp(Widget sender, int x, int y) {
		}
	};

	public ArticleRatingWidget(String articleId, boolean showStatsLabel, boolean showTooltip, HasHorizontalAlignment.HorizontalAlignmentConstant horizontalAlignment, AggregateArticleRating inAtr,Integer vote) {
		this.vote = vote;
		this.articleId = articleId;
		this.showTooltip = showTooltip;
		this.showStatsLabel = showStatsLabel;
		setHorizontalAlignment(horizontalAlignment);
		add(starPanel);
		if (showStatsLabel) {
			if (atr != null) {
				statsLabel = new Label(atr.rating + " rating based on " + atr.numVotes);
			} else {
				statsLabel = new Label("");
			}
			statsLabel.setStyleName("starStatsLabel");
			statsLabel.setWidth("120px");
			add(statsLabel);
		}
		if (inAtr == null) {
			//getArticleRating();
		} else {
			this.atr = inAtr;
			setStars(atr);
		}
	}

	public void starMoused(Widget sender, String imageURL) {
		if (awarded) return;
		if (sender == star1) {
			star1.setUrl(imageURL);
			star1.setTitle("Awful");
			star2.setUrl(starEmptyURL);
			star3.setUrl(starEmptyURL);
			star4.setUrl(starEmptyURL);
			star5.setUrl(starEmptyURL);
		} else if (sender == star2) {
			star1.setUrl(imageURL);
			star2.setUrl(imageURL);
			star2.setTitle("Poor");
			star3.setUrl(starEmptyURL);
			star4.setUrl(starEmptyURL);
			star5.setUrl(starEmptyURL);
		} else if (sender == star3) {
			star1.setUrl(imageURL);
			star2.setUrl(imageURL);
			star3.setUrl(imageURL);
			star3.setTitle("Not bad");
			star4.setUrl(starEmptyURL);
			star5.setUrl(starEmptyURL);
		} else if (sender == star4) {
			star1.setUrl(imageURL);
			star2.setUrl(imageURL);
			star3.setUrl(imageURL);
			star4.setUrl(imageURL);
			star4.setTitle("Good");
			star5.setUrl(starEmptyURL);
		} else if (sender == star5) {
			star1.setUrl(imageURL);
			star2.setUrl(imageURL);
			star3.setUrl(imageURL);
			star4.setUrl(imageURL);
			star5.setUrl(imageURL);
			star5.setTitle("Great");
		}
	}

	public Image setupStar(Image star, String url) {
		if (awarded)return null;
		if (star == null) {
			star = new Image(url);
			star.addClickListener(starClickListener);
			star.addMouseListener(starListener);
			star.setStyleName("ratingStar");
			starPanel.add(star);
		} else {
			star.setUrl(url);
		}
		star.setTitle("");
		return star;
	}

	public void setStars(AggregateArticleRating atr) {
		if (awarded)return;
		String statText = atr.rating + " rating from " + atr.numVotes + " users";
		if (showStatsLabel) {
			statsLabel.setText(statText);
		}
		if (atr.numVotes == 0) {
			star1 = setupStar(star1, starNoVotesURL);
			star2 = setupStar(star2, starNoVotesURL);
			star3 = setupStar(star3, starNoVotesURL);
			star4 = setupStar(star4, starNoVotesURL);
			star5 = setupStar(star5, starNoVotesURL);
		} else {
			float rating = Float.parseFloat(atr.rating);
			if (rating < .25) {
				// 0
				star1 = setupStar(star1, starEmptyURL);
				star2 = setupStar(star2, starEmptyURL);
				star3 = setupStar(star3, starEmptyURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= .25 && rating < .75) {
				// .5
				star1 = setupStar(star1, starHalfURL);
				star2 = setupStar(star2, starEmptyURL);
				star3 = setupStar(star3, starEmptyURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= .75 && rating < 1.25) {
				// 1.0
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starEmptyURL);
				star3 = setupStar(star3, starEmptyURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 1.25 && rating < 1.75) {
				// 1.5
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starHalfURL);
				star3 = setupStar(star3, starEmptyURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 1.75 && rating < 2.25) {
				// 2.0
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starEmptyURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 2.25 && rating < 2.75) {
				// 2.5
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starHalfURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 2.75 && rating < 3.25) {
				// 3.0
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starSelectedURL);
				star4 = setupStar(star4, starEmptyURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 3.25 && rating < 3.75) {
				// 3.5
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starSelectedURL);
				star4 = setupStar(star4, starHalfURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 3.75 && rating < 4.25) {
				// 4.0
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starSelectedURL);
				star4 = setupStar(star4, starSelectedURL);
				star5 = setupStar(star5, starEmptyURL);
			} else if (rating >= 4.25 && rating < 4.75) {
				// 4.5
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starSelectedURL);
				star4 = setupStar(star4, starSelectedURL);
				star5 = setupStar(star5, starHalfURL);
			} else if (rating >= 4.75) {
				// 5
				star1 = setupStar(star1, starSelectedURL);
				star2 = setupStar(star2, starSelectedURL);
				star3 = setupStar(star3, starSelectedURL);
				star4 = setupStar(star4, starSelectedURL);
				star5 = setupStar(star5, starSelectedURL);
			}
		}
		if (showTooltip) {
			star1.setTitle(statText);
			star2.setTitle(statText);
			star3.setTitle(statText);
			star4.setTitle(statText);
			star5.setTitle(statText);
		}
	}
}